/**
 * 解构~Destructuring
 * author:dengyiping
 * date:2016/08/04
 */

/* 1 数组的解构 */
let [foo, [[bar], baz]] = [1, [[2], 3]];
console.log("foo:" + foo + ",bar:"+ bar + ",baz:" + baz);//foo:1,bar:2,baz:3


let [x, , y] = [1, 2, 3];
console.log("x:" + x + ",y:" + y);//x:1,y:3

// let [head, ...tail] = [1, 2, 3, 4];
// console.log("head:" + head + ",tail:" + tail);//head:1,tail:2,3,4

// let [a, b, ...c] = ['a'];
// console.log("a:" + a + ",b:" + b + ",c:" + c);//a:a,b:undefined,c:

/* 2 对象的解构赋值 */
var {uname, pwd} = {uname: "admin",pwd:"aaaaa888"};
console.log("uname:"+uname+",pwd:"+pwd);//uname:admin,pwd:aaaaa888

/* 3 函数参数的解构 + 默认值 */
//undefined就会触发函数参数的默认值
function move({x = 0, y = 0} = {}) {
  console.log([x, y]);
  return [x, y];
}
move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, 0]
move({}); // [0, 0]
move(); // [0, 0]

console.log("-----------");
function move1({x, y} = { x: 0, y: 0 }) {
  console.log([x, y]);
  return [x, y];
}
move1({x: 3, y: 8}); // [3, 8]
move1({x: 3}); // [3, undefined]
move1({}); // [undefined, undefined]
move1(); // [0, 0]